package com.bayair.doctor.ui.main.center;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import com.bayair.doctor.R;
import com.bayair.doctor.adapter.Fragment2Adapter;
import com.bayair.doctor.base.component.BaseFragment;
import com.bayair.doctor.ui.inquiry.InquiryFragment;
import com.bayair.doctor.ui.inquiry_record.InquiryRecordFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import androidx.fragment.app.Fragment;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;
import androidx.viewpager2.widget.ViewPager2;
import butterknife.BindView;

public class CenterFragment extends BaseFragment {

    @BindView(R.id.indicator_center)
    MagicIndicator magicIndicator;
    @BindView(R.id.vp_center)
    ViewPager2 viewPager;

    private final String[] TABS = new String[]{"问诊患者", "问诊记录"};
    private List<Fragment> fragments = new ArrayList<>();
    private List<String> mDataList = Arrays.asList(TABS);

    public static CenterFragment newInstance() {
        CenterFragment myFragment = new CenterFragment();
        return myFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_center;
    }

    @Override
    protected void initView() {
        bindviewpager();
    }

    @Override
    protected void initData() {

    }

    private void bindviewpager() {
        fragments.add(new InquiryFragment());
        fragments.add(new InquiryRecordFragment());
        viewPager.setAdapter(new Fragment2Adapter(this,fragments));

        //设置magicIndicator的样式
        magicIndicator.setBackgroundColor(Color.WHITE);
        CommonNavigator commonNavigator = new CommonNavigator(getContext());
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                //tab样式
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setTextSize(18);
                simplePagerTitleView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                simplePagerTitleView.setNormalColor(Color.GRAY);
                simplePagerTitleView.setSelectedColor(Color.BLACK);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                //指示器样式
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 6));
                indicator.setLineWidth(UIUtil.dip2px(context, 10));
                indicator.setRoundRadius(UIUtil.dip2px(context, 3));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(R.color.colorPrimary);
                return indicator;
            }
        });

        //绑定tab、指示器、viewpager2
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind2(magicIndicator, viewPager);
    }
}
